import {Controller, Get, HttpException, HttpStatus,BadRequestException,NotFoundException,UseFilters} from "@nestjs/common";
import {ForbiddenException} from './forbidden.exception';
import {CustomExceptionFilters} from "./custom-exception.filter";

@Controller('app')
//2. 异常过滤器也可以设置控制器级别的
// @UseFilters(CustomExceptionFilters)
export class AppController {
    @Get("/test")
    testMethod() {
        // // 当异常是未识别的，既不是HttpException 也不是继承自HttpException的类 相应:{"statusCode":500,"message":"Internal server error"}
        // throw new Error("未识别的!");
        // return 'exception filters controller'

        throw new HttpException('httpException: forbidden ', HttpStatus.FORBIDDEN);
        // throw new HttpException({
        //     status: HttpStatus.FORBIDDEN,
        //     error: '自定义的错误消息',
        //     success:false,
        // }, HttpStatus.FORBIDDEN);
    }

    @Get("custom")
    customMethod(){
        throw new ForbiddenException();
    }

    @Get("nest-exception-class")
    nestExceptionClass(){
        throw new BadRequestException('nest-exception-class BadRequestException', 'error参数值');
    }

    @Get("nest-exception-class2")
    nestExceptionClass2(){
        throw new NotFoundException('nest-exception-class NotFoundException', 'error参数值');
    }


    //3. 也可以设置路由方法级别的
    // @UseFilters(new CustomExceptionFilters())  //如果该类中有参数Inject就不能传入实例了
    // @UseFilters(CustomExceptionFilters)  //如果该类中有参数Inject就不能传入实例了
    @Get('CustomExceptionFilters')
    CustomExceptionFiltersMethod(){
        throw new BadRequestException('nest-exception-class BadRequestException', 'error参数值');
    }

    //3. 也可以设置路由方法级别的
    // @UseFilters(CustomExceptionFilters)
    @Get('CustomExceptionFilters2')
    CustomExceptionFiltersMethod2(){
        throw new BadRequestException('nest-exception-class BadRequestException', 'error参数值');
    }
}
